import 'package:flutter/material.dart';
import 'package:flutter_dva/flutter_dva.dart';
import 'package:go_router/go_router.dart';
import 'package:todo_app/components/PreviewImage.dart';
import 'package:todo_app/models/getters.dart';
import 'package:todo_app/pages/group/group-detail-page.dart';
import 'package:todo_app/pages/group/group-qrcode-page.dart';
import 'package:todo_app/pages/group/group-user-page.dart';
import 'package:todo_app/pages/me/aboutus-page.dart';
import 'package:todo_app/pages/me/me-qrcode-page.dart';
import 'package:todo_app/pages/me/me-settings-page.dart';
import 'package:todo_app/pages/message/group-message-page.dart';
import 'package:todo_app/pages/message/single-message-page.dart';
import 'package:todo_app/pages/person/person-qrcode-page.dart';
import 'package:todo_app/pages/todo/todo-collect-list-page.dart';
import 'package:todo_app/pages/todo/todo-detail-page.dart';
import 'package:todo_app/pages/todo/todo-history-list-page.dart';
import 'package:todo_app/pages/todo/todo-publish-page.dart';
import 'package:todo_app/pages/user/user-register-page.dart';

import 'pages/person/person-detail-page.dart';
import 'pages/tabs/tabs-page.dart';
import 'pages/user/user-login-page.dart';


const whiteList = ['/user/sendCode', '/user/login'];
final rootNavigatorState = GlobalKey<NavigatorState>(debugLabel: 'root');
final router = GoRouter(
  navigatorKey: rootNavigatorState,
  routes: [
    // 首页
    GoRoute(
      path: "/",
      name: "home",
      redirect: (_, __) => '/tabs/${tabDataList.first.page}',
    ),
    // tabs
    GoRoute(
      path: '/tabs/:page',
      name: 'tabs',
      builder: (context, state) {
        final page = state.pathParameters['page']!;
        final currentIndex = tabDataList.indexWhere((e) => e.page == page);
        return TabsPage(key: state.pageKey, currentIndex: currentIndex);
      },
    ),
    // 用户登录
    GoRoute(
      path: '/user/login',
      name: 'login',
      builder: (context, state) => const UserLoginPage(),
    ),
    // 用户注册
    GoRoute(
      path: '/user/register',
      name: 'register',
      builder: (context, state) => const UserRegisterPage(),
    ),
    // 发布待办事项
    GoRoute(
      path: '/todo/publish',
      name: 'publishTodo',
      builder: (context, state) => const TodoPublishPage(),
    ),
    // 待办事项详情
    GoRoute(
      path: '/todo/detail/:id',
      name: 'todoDetail',
      builder: (context, state) => TodoDetailPage(id: state.pathParameters['id']!,),
    ),
    // 分组详情
    GoRoute(
      path: '/group/detail/:id',
      name: 'groupDetail',
      builder: (context, state) {
        return const GroupDetailPage();
      }
    ),
    // 分组消息
    GoRoute(
      path: '/message/group/:id',
      name: 'groupMessageRoom',
      builder: (context, state) {
        return const GroupMessagePage();
      }
    ),
    // 单人消息
    GoRoute(
      path: '/message/single/:id',
      name: 'singleMessageRoom',
      builder: (context, state) {
        return const SingleMessagePage();
      }
    ),
    // 分组二维码
    GoRoute(
      path: '/group/qrcode/:groupNo',
      name: 'groupQrcode',
      builder: (context, state) => GroupQrcodePage(groupNo: state.pathParameters['groupNo']!,),
    ),
    // 分组成员
    GoRoute(
      path: '/group/users/:id',
      name: 'groupUsers',
      builder: (context, state) => GroupUserPage(id: state.pathParameters['id']!,),
    ),
    // 设置
    GoRoute(
      path: '/me/settings',
      name: 'meSettings',
      builder: (context, state) => const MeSettingsPage(),
    ),
    // 关于我们
    GoRoute(
      path: '/me/aboutUs',
      name: 'aboutUs',
      builder: (context, state) => const AboutUsPage(),
    ),
    // 我的二维码
    GoRoute(
      path: '/me/qrcode',
      name: 'meQrcode',
      builder: (context, state) => const MeQrcodePage(),
    ),
    // 用户详情
    GoRoute(
      path: '/person/detail/:id',
      name: 'personDetail',
      builder: (context, state) => PersonDetailPage(userId: state.pathParameters['id']!,),
    ),
    // 用户二维码
    GoRoute(
      path: '/person/qrcode/:id',
      name: 'personQrcode',
      builder: (context, state) => PersonQrcodePage(userId: state.pathParameters['id']!,),
    ),
    // 收藏
    GoRoute(
      path: '/todo/collect/list',
      name: 'todoCollectList',
      builder: (context, state) => const TodoCollectListPage(),
    ),
    // 历史待办
    GoRoute(
      path: '/todo/history/list',
      name: 'todoHistoryList',
      builder: (context, state) => const TodoHistoryListPage(),
    ),
    // 图片预览
    GoRoute(
        path: '/previewImage',
        name: 'previewImage',
        builder: (context, state) =>
            PreviewImage(photoURL: state.extra as String)),
    // 图片预览
    GoRoute(
      path: '/previewImages',
      name: 'previewImages',
      builder: (context, state) {
        var arguments = state.extra as Map<String, dynamic>;
        return PreviewImages(
            urls: arguments['urls'] as List<String>,
            index: arguments['index']);
      }),
  ],
  redirect: (context, state) {
    if (whiteList.contains(state.location)) {
      return null;
    }
    if (Provider().store!.rootState.system!.loginSuccess != true) {
      return '/user/login';
    } else {
      return null;
    }
  },
);
